'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数	
	const collection = db.collection('page-home-list')
	const type = event.dataType
	if (type == 'add') {
		const publishName = event.userName;
		const publishNickName = event.nickName;
		const publishUserId = event.userId;
		const publishId = getPublishId();
		const publishDescription = event.des;	
		const area = event.area;
		const houseState = event.houseState;
		const pageMode = event.pageMode;
		const rentTime = event.rentTime;		
		const rentMoney = event.rentMoney;
		const transferMoney = event.transferMoney;
		const provinceName = event.provinceName;
		const provinceCode = event.provinceCode;
		const cityName = event.cityName;
		const cityCode = event.cityCode;
		const districtName = event.districtName;
		const districtCode = event.districtCode;
		const address = event.address;
		const phone = event.phone;
		const publishDate = event.publishDate;
		const publishImage = event.publishImage
		const res = await collection.add({
			publishName,
			publishNickName,
			publishUserId,
			publishId,
			publishDescription,
			area,
			houseState,
			pageMode,
			rentTime,
			rentMoney,
			transferMoney,
			provinceName,
			provinceCode,
			cityName,
			cityCode,
			districtName,
			districtCode,
			address,
			phone,
			publishDate,
			publishImage
		})
		return {
			code: 0,
			msg: res
		}
	} else if (type == 'query') {
		const res = await collection.get()
		return {
			code: 0,
			list: res.data
		}
	} else if (type == 'queryBy') {
		const publishId = event.publishId;
		const res = await collection.where({
			publishId: publishId
		}).get()
		return {
			code: 0,
			list: res.data
		}
	} else if (type == 'queryByUserName') {
		const userName = event.userName;
		const res = await collection.where({
			publishName: userName
		}).get()
		return {
			code: 0,
			list: res.data
		}
	} else if (type == 'deleteById') {
		const publishId = event.publishId;
		const res = await collection.where({
			publishId: publishId
		}).remove()
		return {
			code: 0,
			list: res.data
		}
	}
};

function getPublishId() {
	return 'id_' + Math.random().toString(36).substr(2, 9);
}